<?php
require_once('wpfunctions.php');
header("Content-type: application/json");
if(!ip_isLoggedIn()) {
  return '{"error":"Please log in first."}';
}
function dirToArray($dir) { 
   
   $result = array(); 

   $cdir = scandir($dir); 
   foreach ($cdir as $key => $value) 
   { 
      if (!in_array($value,array(".",".."))) 
      { 
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
         { 
            $result[] = array('name'=>$value, 'children'=>dirToArray($dir . DIRECTORY_SEPARATOR . $value)); 
         } 
         else 
         { 
            $result[] = array('name'=>$value);
         } 
      } 
   } 
   
   return $result; 
}
echo json_encode(dirToArray('library' . DIRECTORY_SEPARATOR . $_REQUEST['path']));
?>